﻿
// ================================================================================
//        File: frmMemberAppend.cs
//        Desc: 添加/修改员工
//        Auth: JF
//        Date: 2010-10-12
// ================================================================================
//        Change History
// ================================================================================
//        Date:         Author:                  Description:
//        --------     --------              -------------------
//      11/01           JF                  1,取消对生日时间的限制,修改生日只要输入月,日.              
// ================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EShop.BLL;
using EShop.Model;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmMemberAppend : frmBase
    {
        public frmMemberAppend()
        {
            InitializeComponent();
            isAppend = true;
        }
        public frmMemberAppend(string strMemberID)
        {
            InitializeComponent();
            this.strMemberID = strMemberID;
            isAppend = false;

        }

        private string strMemberID = string.Empty;

        private bool isAppend = true;

        MemberManagerBLL oMemberManagerBLL = new MemberManagerBLL();
        private void frmMemberAppend_Load(object sender, EventArgs e)
        {
            this.Text = isAppend ? "[eShop]会员添加" : "[eShop]会员查看/修改";
            this.btnSumbit.Text = isAppend ? "提交" : "查看/修改完成";
            this.txtMemberID.Enabled = isAppend ? true : false;

            FillLevel();
            btnClear_Click(sender, e);
        }

        private void Init()
        {
            
            DataTable dt=oMemberManagerBLL.GetMember(strMemberID,0);
            this.txtMemberID.Text = strMemberID;
            this.txtMemberName.Text = dt.Rows[0]["MemberName"].ToString();
            this.cbbSex.SelectedIndex = Convert.ToInt32(dt.Rows[0]["MemberSex"]);
            this.dtBrithday.Value = (DateTime)dt.Rows[0]["Brithday"];
            this.cbxIsGregorianCalendar.Checked = (bool)dt.Rows[0]["IsGregorianCalendar"];
            int iLevelID = Convert.ToInt32(dt.Rows[0]["LevelID"]);
            //foreach (ComboBox.ObjectCollection item in cbbLevel.Items)
            //{
            //    Model.MemberLevelInfo info = (MemberLevelInfo)item;
            //    if(
            //}
            for (int i = 0; i < cbbLevel.Items.Count; i++)
            {
                Model.MemberLevelInfo info = (MemberLevelInfo)cbbLevel.Items[i];
                if (info.LevelID.Equals(iLevelID.ToString()))
                {
                    this.cbbLevel.SelectedIndex = i;
                    break;
                }
            }

            //this.cbbLevel.SelectedIndex = (int)dt.Rows[0]["LevelID"];///
            this.txtAddress.Text = (string)dt.Rows[0]["MemberAddress"];
            this.txtMobile.Text = (string)dt.Rows[0]["MemberMobel"];
            this.rtbRemark.Text = (string)dt.Rows[0]["Remark"];

        }

        private void FillLevel()
        {
            this.cbbLevel.Items.Clear();
            DataTable dt = oMemberManagerBLL.GetLevelInfo();
            int rows = dt.Rows.Count;
            for (int i = 0; i < rows; i++)
            {
                cbbLevel.Items.Add(new MemberLevelInfo(dt.Rows[i]["LevelID"].ToString(),
                    dt.Rows[i]["LevelName"].ToString(),
                    Convert.ToDecimal(dt.Rows[i]["LevelDiscount"])));
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (isAppend) Reset();
            else Init();
        }
        private void Reset()
        {
            txtMemberID.Clear();
            txtMemberName.Clear();
            cbbSex.SelectedIndex = 1;
            dtBrithday.Value = new System.DateTime(1990, 1, 1, 0, 0, 0, 0);
            try
            {
                cbxIsGregorianCalendar.Checked = false;
            }
            catch { }
            cbbLevel.SelectedIndex = 0;
            txtMobile.Clear();
            txtAddress.Clear();
            rtbRemark.Clear();
            panel4.Visible = false;
        }

        private void btnSumbit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtMemberID.Text))
            {
                this.panel4.Visible = true;
                lblMsg.Text = "请输入新会员卡号";
                this.txtMemberID.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtMemberName.Text))
            {
                this.panel4.Visible = true;
                lblMsg.Text = "请输入新会员姓名";
                this.txtMemberName.Focus();
                return;
            }
            //if (dtBrithday.Value > DateTime.Now.AddYears(-1))
            //{
            //    this.panel4.Visible = true;
            //    lblMsg.Text = "会员生日异常";
            //    return;
            //}
            if (cbbLevel.Items.Count <= 0)
            {
                this.panel4.Visible = true;
                lblMsg.Text = "请添加会员等级信息";
                return;
            }
            byte sex = Convert.ToByte(cbbSex.SelectedIndex);
            byte IsGregorianCalendar = Convert.ToByte(cbxIsGregorianCalendar.Checked);
            int item = Convert.ToInt32(((MemberLevelInfo)cbbLevel.Items[cbbLevel.SelectedIndex]).LevelID);
            if (isAppend)
            {//append
                if (oMemberManagerBLL.MemberInfoAppend(SystemLibrary.SQLQuoteName(txtMemberID.Text.Trim()), SystemLibrary.SQLQuoteName(txtMemberName.Text), sex, dtBrithday.Value,
                    IsGregorianCalendar, item,
                    SystemLibrary.SQLQuoteName(txtMobile.Text), SystemLibrary.SQLQuoteName(txtAddress.Text), SystemLibrary.SQLQuoteName(rtbRemark.Text)) &&
                    oMemberManagerBLL.MemberScoreAppend(SystemLibrary.SQLQuoteName(txtMemberID.Text.Trim()), 0, 0, DateTime.Now))
                {
                    MessageBox.Show("添加成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                //edit
                if (oMemberManagerBLL.MemberInfoUpdate(txtMemberID.Text.Trim(), txtMemberName.Text, sex, dtBrithday.Value,
                    IsGregorianCalendar, item,
                    txtMobile.Text, txtAddress.Text, rtbRemark.Text))
                {
                    MessageBox.Show("修改成功");
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
            
        }

    }
}
